<template>
  <div class="kangyangBox">
    <div class="yuanze">
      <div class="icon-title">
        <span class="icon"></span>
        <span class="t">三济康养原则</span>
      </div>
      <div class="desc">{{state.foodData.generalRule}}</div>
    </div>
    <div class="kangTab">
      <div :class="['ktab', state.acTab===i?'acKtab':'']" v-for="(t, i) in state.tabList" :key="i" @click="changeTab(i)">
        <img :src="getAssetImg(t.icon)" alt="" class="kImg">
        <div class="name">{{t.name}}</div>
      </div>
    </div>
    <food-kang v-show="state.acTab===0" :foodData="state.foodData" :loadErr="state.loadErr" @toastFather="getDiet"></food-kang>
    <sport-kang v-show="state.acTab===1" :doLoad="state.tabLoads[1]" @loadFaild="reSetComponentRequest"></sport-kang>
    <qing-zhi-kang v-show="state.acTab===2" :doLoad="state.tabLoads[2]" @loadFaild="reSetComponentRequest"></qing-zhi-kang>
    <jing-luo-kang v-show="state.acTab===3" :doLoad="state.tabLoads[3]" :acTab="state.acTab" @loadFaild="reSetComponentRequest"></jing-luo-kang>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive, watch, toRefs } from 'vue';
import { useRoute } from 'vue-router';
import { Toast } from 'vant';
import {http} from "@/axios/http";
import * as API from "@/axios/api";
import utils from '@/utils/utils.ts';
import FoodKang from './food-kang.vue'
import SportKang from './sport-kang.vue'
import QingZhiKang from './qingzhi-kang.vue'
import JingLuoKang from './jingluo-kang.vue'

const route = useRoute();
let checkQueryResult = ref(true)
let state = reactive({
  tabList: [
    {icon: '../../../assets/images/icon/yinshiTab.png', name: '饮食调理'},
    {icon: '../../../assets/images/icon/yundongTab.png', name: '运动调理'},
    {icon: '../../../assets/images/icon/qingzhiTab.png', name: '情志调理'},
    {icon: '../../../assets/images/icon/jingluoTab.png', name: '经络调理'}
  ],
  acTab: 0,
  tabLoads: [false, false, false, false], // 控制tab加载
  foodData: {},
  loadErr: false
})

let props = defineProps<{
  doLoad: boolean
}>()
let emit = defineEmits(['fatherLoad'])
let { doLoad } = toRefs(props);
onMounted(() => {
  checkQueryResult.value = utils.checkRouterParams(route.query, false)
})

watch(() => state.acTab, (newVal, oldVal) => {
  if (newVal === 0 && !state.tabLoads[0]) {
    getDiet()
  }
})

watch(doLoad, (newVal, oldVal) => {
  if (newVal && !state.tabLoads[0] && state.acTab===0) {
    getDiet()
  }
})
let getAssetImg = (src) => {
  return new URL(src, import.meta.url).href
}
let reSetComponentRequest = (k) => {
  state.tabLoads[k] = false
}
let getDiet = () => {
  Toast.loading({
    mask: false,
    message: '',
    duration: 0
  })
  http({
    baseUrl: API.baseUrl,
    url: API.getDiet,
    method: 'GET',
    params: {
      userId: route.query.userId as string
    }
  },(res) =>{
    Toast.clear()
    state.loadErr = false
    if (res && res.data && res.data.code === 200){
      state.tabLoads[0] = true
      state.foodData = res.data.data || {}
    } else {
      reSetComponentRequest(0)
      emit('fatherLoad')
      utils.toastMsg(res, state, API.getDiet)
    }
  })
}

let changeTab = (k) => {
  if (state.acTab!==k) {
    state.acTab = k
    if (!state.tabLoads[k] && checkQueryResult.value && k > 0) {
      state.tabLoads[k] = true
    }
  }
}
</script>

<style lang="scss">
.kangyangBox{
  width: 355px;
  margin: 0 auto;
  .yuanze{
    min-height: 84px;
    background: linear-gradient(155deg, #F3FEFF 0%, #DCECFF 100%), linear-gradient(202deg, rgba(181,223,254,0.8600) 0%, rgba(255,255,255,0) 100%);
    border-radius: 8px;
    padding: 12px;
    text-align: left;
    position: relative;
    margin-bottom: 12px;
    &:before{
      position: absolute;
      content: '';
      left: 0;
      top: 0;
      width: 100%;
      height: 84px;
      background-image: url("../../../assets/images/report/yuanze-bg.png");
      background-size: 226px 84px;
      background-position: left top;
      background-repeat: no-repeat;
    }
    .icon-title{
      display: flex;
      align-items: center;
      .t{
        color: #2259A4;
        font-size: 15px;
        font-weight: bold;
      }
      .icon{
        width: 3px;
        height: 9px;
        background: #4D96FA;
        border-radius: 2px;
        margin-right: 6px;
      }
    }
    .desc{
      line-height: 17px;
      color: #333333;
      font-size: 13px;
      margin-top: 4px;
    }
  }
  .kangTab{
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #333333;
    font-size: 14px;
    margin-bottom: 10px;
    .ktab{
      width: 78px;
      height: 52px;
      background-color: #ffffff;
      text-align: center;
      box-shadow: 0px 2px 6px 0px rgba(145,149,161,0.1600);
      //display: flex;
      //flex-direction: column;
      //align-items: center;
      .kImg{
        width: 46px;
        height: 32px;
      }
      .name{
        margin-top: -10px;
      }
    }
    .acKtab{
      height: 57px;
      background: url("../../../assets/images/icon/kangTab.png") no-repeat;
      background-size: 78px 57px;
      color: #4B7AFF;
      font-weight: 600;
      box-shadow: none;
    }
  }
  .foodCard{
    width: 355px;
    background: #FFFFFF;
    box-shadow: 0px 2px 6px 0px rgba(145,149,161,0.1600);
    border-radius: 8px;
    border: 1px solid #E6ECFF;
    margin-bottom: 12px;
    text-align: left;
    padding: 12px 10px 0;
  }

  .s-li{
    line-height: 17px;
    margin-bottom: 12px;
    position: relative;
    margin-left: 12px;
    &:last-child{
      margin-bottom: 0;
    }
    &:before{
      content: '';
      position: absolute;
      width: 6px;
      height: 6px;
      background: #AFCED9;
      border-radius: 3px;
      left: -12px;
      top: 6px;
    }
  }
  .s-tip{
    background: #F2FCFF;
    border-radius: 4px;
    color: #333333;
    line-height: 17px;
    padding: 10px;
    margin-bottom: 10px;
    p{
      margin-bottom: 5px;
      &:last-child{
        margin-bottom: 0;
      }
    }
  }
  .jl-li{
    margin-bottom: 13px;
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    .jl-name{
      width: 67px;
      margin-right: 10px;
      line-height: 26px;
      background: #F3F6FF;
      border-radius: 0px 8px 0px 8px;
      font-size: 14px;
      font-weight: 500;
      color: #3654A9;
      text-align: center;
      flex:none;
    }
    .jl-text{
      font-size: 14px;
      font-weight: 400;
      color: rgba(34,34,34,0.8000);
      line-height: 20px;
      flex:1;
    }
  }
}
</style>
